package com.ruoyi.userMission.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.floorMission.mapper.HsFloorMissionMapper;
import com.ruoyi.household.mapper.HsHouseholdMapper;
import com.ruoyi.userMission.domain.ro.FloorToUserMissionRo;
import com.ruoyi.userMission.domain.vo.GetUserMissionStatusVo;
import com.ruoyi.userMission.domain.vo.MissionNameVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.userMission.mapper.HsUserMissionMapper;
import com.ruoyi.userMission.domain.HsUserMission;
import com.ruoyi.userMission.service.IHsUserMissionService;

/**
 * 用户核酸任务管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-29
 */
@Service
public class HsUserMissionServiceImpl implements IHsUserMissionService 
{
    @Autowired
    private HsUserMissionMapper hsUserMissionMapper;
    @Autowired
    private HsFloorMissionMapper hsFloorMissionMapper;
    @Autowired
    private HsHouseholdMapper hsHouseholdMapper;
    /**
     * 查询用户核酸任务管理
     * 
     * @param id 用户核酸任务管理主键
     * @return 用户核酸任务管理
     */
    @Override
    public HsUserMission selectHsUserMissionById(Long id)
    {
        return hsUserMissionMapper.selectHsUserMissionById(id);
    }

    /**
     * 查询用户核酸任务管理列表
     * 
     * @param hsUserMission 用户核酸任务管理
     * @return 用户核酸任务管理
     */
    @Override
    public List<HsUserMission> selectHsUserMissionList(HsUserMission hsUserMission)
    {
        return hsUserMissionMapper.selectHsUserMissionList(hsUserMission);
    }

    /**
     * 新增用户核酸任务管理
     * 
     * @param hsUserMission 用户核酸任务管理
     * @return 结果
     */
    @Override
    public int insertHsUserMission(HsUserMission hsUserMission)
    {

        hsUserMission.setUserMissionStatus(0);
        hsUserMission.setUserMissionStart( hsFloorMissionMapper.getStartTime(hsUserMission.getUserMissionName()));
        hsUserMission.setUserMissionEnd( hsFloorMissionMapper.getEndTime(hsUserMission.getUserMissionName()));
        String userMissionName = hsUserMission.getUserMissionName();
        hsUserMission.setUserMissionPosition(hsFloorMissionMapper.getPosition(userMissionName));
        String[] split = userMissionName.split("日");
        hsUserMission.setUserMissionName(hsUserMission.getUserMissionXq()+split[0]+"日"+hsUserMission.getUserMissionUsername()+split[1]);
        return hsUserMissionMapper.insertHsUserMission(hsUserMission);

    }

    /**
     * 修改用户核酸任务管理
     * 
     * @param hsUserMission 用户核酸任务管理
     * @return 结果
     */
    @Override
    public int updateHsUserMission(HsUserMission hsUserMission)
    {
        return hsUserMissionMapper.updateHsUserMission(hsUserMission);
    }

    /**
     * 批量删除用户核酸任务管理
     * 
     * @param ids 需要删除的用户核酸任务管理主键
     * @return 结果
     */
    @Override
    public int deleteHsUserMissionByIds(Long[] ids)
    {
        return hsUserMissionMapper.deleteHsUserMissionByIds(ids);
    }

    /**
     * 删除用户核酸任务管理信息
     * 
     * @param id 用户核酸任务管理主键
     * @return 结果
     */
    @Override
    public int deleteHsUserMissionById(Long id)
    {
        return hsUserMissionMapper.deleteHsUserMissionById(id);
    }

    @Override
    public List<GetUserMissionStatusVo> getUMissionStatus() {
        List<GetUserMissionStatusVo> allFloor = hsUserMissionMapper.getAllStatus();
        return allFloor;
    }

    @Override
    public List<MissionNameVo> getMissionName() {
        List<MissionNameVo> allFloor = hsUserMissionMapper.getAllMissionName();
        return allFloor;
    }

    @Override
    public int addFloorUserMission(FloorToUserMissionRo floorToUserMissionRo) {
        String[] household = hsHouseholdMapper.getHousehold(floorToUserMissionRo.getFloorMissionFloorname(), floorToUserMissionRo.getFloorMissionXqname());
        for(int i = 0 ; i<household.length; i++){
            HsUserMission hsUserMission = new HsUserMission();
            hsUserMission.setUserMissionFloorname(floorToUserMissionRo.getFloorMissionFloorname());
            hsUserMission.setUserMissionXq(floorToUserMissionRo.getFloorMissionXqname());
            hsUserMission.setUserMissionStart(floorToUserMissionRo.getFloorMissionStart());
            hsUserMission.setUserMissionEnd(floorToUserMissionRo.getFloorMissionEnd());
            hsUserMission.setUserMissionPosition(floorToUserMissionRo.getFloorMissionPosition());
            hsUserMission.setUserMissionUsername(household[i]);
            hsUserMission.setUserMissionStatus(0);
            Date floorMissionStartTime = floorToUserMissionRo.getFloorMissionStart();
            //得到日期中的日
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            // 使用ISO_LOCAL_DATE_TIME格式解析日期时间
            String dateTimeString = dateFormat.format(floorMissionStartTime);
            // 将转换为的字符串进行拆分
            String[] split = dateTimeString.split("-");
            // 输出年月日
            hsUserMission.setUserMissionName(split[0]+"年"+split[1]+"月"+split[2]+"日"+floorToUserMissionRo.getFloorMissionFloorname()+household[i]+floorToUserMissionRo.getFloorMissionFloorname());
            hsUserMissionMapper.insertHsUserMission(hsUserMission);
        }
        return 1;
    }
}
